package DHT.Chord.OpenChord.SimpleDHTTests;

import java.net.MalformedURLException;

import DHT.Chord.OpenChord.DHTFurtherClass.StringKey;

import de.uniba.wiai.lspi.chord.data.URL;
import de.uniba.wiai.lspi.chord.service.Chord;
import de.uniba.wiai.lspi.chord.service.ServiceException;

public class MainTestSecondNode {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		//PropertiesLoader.loadPropertyFile ();
		System.setProperty("log4j.properties.file", "log4j.properties");
		System.setProperty("de.uniba.wiai.lspi.chord.data.ID.number.of.displayed.bytes", "4");
		System.setProperty("de.uniba.wiai.lspi.chord.data.ID.displayed.representation", "2");
		System.setProperty("de.uniba.wiai.lspi.chord.service.impl.ChordImpl.successors", "2");
		System.setProperty("de.uniba.wiai.lspi.chord.service.impl.ChordImpl.AsyncThread.no", "10");
		System.setProperty("de.uniba.wiai.lspi.chord.service.impl.ChordImpl.StabilizeTask.start", "12");
		System.setProperty("de.uniba.wiai.lspi.chord.service.impl.ChordImpl.StabilizeTask.interval", "12");
		System.setProperty("de.uniba.wiai.lspi.chord.service.impl.ChordImpl.FixFingerTask.start", "0");
		System.setProperty("de.uniba.wiai.lspi.chord.service.impl.ChordImpl.FixFingerTask.interval", "12");
		System.setProperty("de.uniba.wiai.lspi.chord.service.impl.ChordImpl.CheckPredecessorTask.start", "6");
		System.setProperty("de.uniba.wiai.lspi.chord.service.impl.ChordImpl.CheckPredecessorTask.interval", "12");
		System.setProperty("de.uniba.wiai.lspi.chord.com.socket.InvocationThread.corepoolsize", "10");
		System.setProperty("de.uniba.wiai.lspi.chord.com.socket.InvocationThread.maxpoolsize", "50");
		System.setProperty("de.uniba.wiai.lspi.chord.com.socket.InvocationThread.keepalivetime", "20");
		
		String protocol = URL.KNOWN_PROTOCOLS.get(URL.SOCKET_PROTOCOL );
		
		URL localURL = null;
		
		try {
			
			localURL = new URL ( protocol + " ://localhost:8181/ ");
			
		} catch ( MalformedURLException e){
			
			throw new RuntimeException (e);
			
		}

		URL bootstrapURL = null;
		
		try {
			
			bootstrapURL = new URL ( protocol + " ://localhost:8080/ ");
		
		} catch ( MalformedURLException e){
			
			throw new RuntimeException (e);
		
		}
		
		Chord chord = new de.uniba.wiai.lspi.chord.service.impl.ChordImpl();
		
		try {
			
			chord.join ( localURL , bootstrapURL );
		
		} catch ( ServiceException e) {
			
			throw new RuntimeException (" Could not join DHT !", e);
		
		}
		
		String data = " Just an example .";
		
		StringKey myKey = new StringKey ( data );
		
		try{
			
			chord.insert (myKey , data );
		
		} catch( ServiceException e){
			e.printStackTrace();
		}
		
		try {

			System.out.println("\n\n\n\n\n");
			System.out.println(chord.retrieve(myKey));
			System.out.println("\n\n\n\n\n");
			
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		
	}

}
